package com.incross.dawin.util;

import java.io.File;
import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageButton;

import com.incross.dawin.util.BKImageDownloaderTask.OnBitmapDownFinishListener;

public class BKImageDownloader implements com.incross.dawin.util.BKImageDownloaderTask.OnDownloadFinishListener, OnBitmapDownFinishListener {
	
	public Drawable mLoadingImage;
	
	private Context mContext;
	
	private String CACHE_PATH = null;
	
	private OnDownloadFinishListener mListener;
	private OnBitmapDownloadFinishListener mBitmapDownloadFinishListener;
	
	public interface OnDownloadFinishListener {
		public abstract void onDownloadFinished(boolean result, String requestedUrl);
	}
	
	public interface OnBitmapDownloadFinishListener {
		public abstract void onBitmapDownloadFinished(ArrayList<Bitmap> bitmaps);
	}
	
	public BKImageDownloader(Context context) {
		mContext = context;
		if(CommonUtils.getStringPreference(mContext, "cache_path_new").equals("")) {
			CACHE_PATH = mContext.getCacheDir().getAbsolutePath() + "/icon/";
			CommonUtils.setStringPreference(mContext, "cache_path_new", CACHE_PATH);
		} else {
			CACHE_PATH = CommonUtils.getStringPreference(mContext, "cache_path_new");
		}
		
		checkCacheFolder();
	}
	
	public void setOnDownloadFinishListener(OnDownloadFinishListener l) {
		mListener = l;
	}
	
	public void setOnBitmapDownloadFinishListener(OnBitmapDownloadFinishListener l) {
		mBitmapDownloadFinishListener = l;
	}
	
	private void checkCacheFolder() {
		File iconFolder = new File(CACHE_PATH);
		
		if(!iconFolder.exists()) {
			BKLog.d("folder do not exist, folder path=" + CACHE_PATH);
			iconFolder.mkdir();
		} else {
			BKLog.d("folder exist, folder path=" + CACHE_PATH);
		}
	}
	
	
	
	/*
	 * Download image and save into chche when image not exist.
	 * @param url : full url with filename.(ex:http://112.169.59.42:8088/common/images/app/face.png)
	 * @param imageButton :  
	 * @return	image cache or not(true: already downloaded, false: need image download.
	 */
	public boolean download(String url, ImageButton button, boolean isShowDirectly) {
		// url에서 파일 이름을 파싱한다.(파일 이름이 "시분초.jpg"이므로 상위 폴더부터 파일이름으로 처리하며, "/"는 "-"로 치환한다.)
		// 시분초만 가지고 처리하면 이미지가 중복될 수 있음.
		if(url == null || url.equals("")) {
			return false;
		}
		
		int index = url.lastIndexOf("/");
		String filename = url.substring(index + 1);
		
		
		File file = new File(CACHE_PATH + filename);
		
		if(file.exists()) {
			Bitmap bitmap = BKImageDownloaderTask.getBitmap(CACHE_PATH + filename);
			if(button != null) {
				
				button.setBackgroundDrawable(new BitmapDrawable(bitmap));
				
				/* ###### Ben, Temp code */
				if(isShowDirectly) {
					button.setVisibility(View.VISIBLE);
				}
				
			}
			
			
			return true;
		} else {
			BKImageDownloaderTask task = new BKImageDownloaderTask(url, filename, CACHE_PATH);
			task.downloadImage(button, isShowDirectly);
		}
		return false;
	}
	
	public boolean download(String url) {
		if(url == null || url.equals("")) return false;
		int index = url.lastIndexOf("/");
		String filename = url.substring(index + 1);
		
		File file = new File(CACHE_PATH + filename);
		
		if(file.exists()) {
			return true;
		} else {
			BKImageDownloaderTask task = new BKImageDownloaderTask(url, filename, CACHE_PATH);
			task.downloadImage();
		}
		
		return false;
	}
	
	public int downloadBitmaps(ArrayList<String> bitmapUrlArray) {
		
		BKImageDownloaderTask task= new BKImageDownloaderTask(mContext, bitmapUrlArray);
		task.setOnBitmapDownloadFinishListener(new OnBitmapDownFinishListener() {
			
			@Override
			public void onBitmapDownloadFinished(boolean result, ArrayList<Bitmap> bitmapArray) {
				if(mBitmapDownloadFinishListener != null) {
					mBitmapDownloadFinishListener.onBitmapDownloadFinished(bitmapArray);
				}
			}
		});
		task.downloadImage();
		
		return 0;
	}
	
	public boolean isExists(String url) {
		if(url == null || url.equals("")) return false;
		int index = url.lastIndexOf("/");
		String filename = url.substring(index + 1);
		
		File file = new File(CACHE_PATH + filename);
		if(file.exists()) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public void onDownloadFinished(boolean result, String requestedUrl) {
		if(mListener != null) {
			mListener.onDownloadFinished(result, requestedUrl);
		}
	}

	@Override
	public void onBitmapDownloadFinished(boolean result, ArrayList<Bitmap> bitmapArray) {
		if(result && mBitmapDownloadFinishListener != null) {
			mBitmapDownloadFinishListener.onBitmapDownloadFinished(bitmapArray);
		}
	}
	

}
	
